import React from 'react'

import RealTimeCodeEditor from '@/components/amis/RealTimeCodeEditor/RealTimeCodeEditor'
import { HiddenControlSchema } from 'amis/lib/renderers/Form/Hidden'
import { TextControlSchema } from 'amis/lib/renderers/Form/InputText'
import { DateTimeControlSchema } from 'amis/lib/renderers/Form/InputDate'

import { ApiObject } from 'amis-core/lib/types'
import rsqlUtils from '@/core/utils/rsql/rsqlUtils'
import coreConstants from '@/core/constants/coreConstants'
import { RsqlDataOperator } from '@/core/utils/rsql/types/RsqlDataOperator'
import { RsqlDataOperatorEnum } from '@/core/utils/rsql/types/RsqlOperatorEnum'
import defaultCrudTableUtils from '@/components/DefaultCrudTable/defaultCrudTableUtils'
import idOfClientSelectUtils from '@/components/oauth2/idOfClientSelectUtils'
import { SelectControlSchema } from 'amis/lib/renderers/Form/Select'
import idOfClientColumnUtils from '@/components/oauth2/idOfClientColumnUtils'
import oauth2AdminApiConfig from '@/core/oauth2/generated/apiconfig/oauth2AdminApiConfig'

const Oauth2AuthorizationList = () => {
	const entityName = '客户端认证的令牌'
	const isModifyModeExpression = '${isEditMode}'
	const modelApiConfig = oauth2AdminApiConfig.oauth2Authorization
	let schema = defaultCrudTableUtils.getSchema({
		entityTitle: `${entityName}`,
		searchServerApi: {
			apiConfig: modelApiConfig.search,
			requestAdaptor: (api: ApiObject, context: any): ApiObject | Promise<ApiObject> => {
				const operators: RsqlDataOperator[] = [
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'id',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'registeredClientId',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'principalName',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'accessTokenValue',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'deleteFlag',
					} as RsqlDataOperator,
				]
				context[coreConstants.deleteFlagFieldName] = coreConstants.notDeleteFlag
				api = rsqlUtils.addRsqlFilter(api, context, coreConstants.api.queryFilterName, operators)
				return api
			},
		},
		addServerApi: undefined,
		deleteServerApi: {
			apiConfig: modelApiConfig.removeLogic,
		},
		detailServerApi: {
			apiConfig: modelApiConfig.get,
			data: {
				id: '${id}',
			},
		},
		editServerApi: undefined,
		debug: false,
		formItems: [
			{
				type: 'hidden',
				name: 'id',
				label: 'ID',
			} as HiddenControlSchema,
			{
				type: 'input-text',
				name: 'registeredClientId',
				label: '已注册的客户端id',
				required: false,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'principalName',
				label: '认证名称',
				required: false,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'authorizationGrantType',
				label: '授权方式',
				required: false,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'authorizationScopes',
				label: '授权范围',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'attributes',
				label: '授权扩展属性',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'state',
				label: '授权state',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'authorizationCodeValue',
				label: '授权code',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'authorizationCodeIssuedAt',
				label: '授权code发布时间',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'authorizationCodeExpiresAt',
				label: '授权code过期时间',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'authorizationCodeMetadata',
				label: '授权code元数据',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'accessTokenValue',
				label: '授权access token',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'accessTokenIssuedAt',
				label: '授权access token发布时间',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'accessTokenExpiresAt',
				label: '授权access token过期时间',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'accessTokenMetadata',
				label: '授权access token元数据',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'accessTokenType',
				label: 'access token类型',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'accessTokenScopes',
				label: 'access token授权范围',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'oidcIdTokenValue',
				label: '授权oidcId token',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'oidcIdTokenIssuedAt',
				label: '授权oidcId token发布时间',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'oidcIdTokenExpiresAt',
				label: '授权oidcId token过期时间',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'oidcIdTokenMetadata',
				label: '授权oidcId token元数据',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'refreshTokenValue',
				label: '授权refresh token',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'refreshTokenIssuedAt',
				label: '授权refresh token发布时间',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'refreshTokenExpiresAt',
				label: '授权refresh token过期时间',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-text',
				name: 'refreshTokenMetadata',
				label: '授权refresh token元数据',
				required: true,
				disabledOn: isModifyModeExpression,
			} as TextControlSchema,
			{
				type: 'input-datetime',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
			{
				type: 'input-datetime',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
		],
		columns: [
			{
				name: 'id',
				label: 'id',
				searchable: true,
			},
			{
				...idOfClientColumnUtils.getSchema({ name: 'registeredClientId' } as SelectControlSchema),
				searchable: idOfClientSelectUtils.getSchema({ name: 'registeredClientId' } as SelectControlSchema),
			},
			{
				name: 'principalName',
				label: '认证名称',
				searchable: true,
			},
			{
				name: 'authorizationGrantType',
				label: '授权方式',
			},
			{
				name: 'authorizationScopes',
				label: '授权范围',
			},
			{
				name: 'attributes',
				label: '授权扩展属性',
			},
			{
				name: 'state',
				label: '授权state',
			},
			{
				name: 'authorizationCodeValue',
				label: '授权code',
			},
			{
				name: 'authorizationCodeIssuedAt',
				label: '授权code发布时间',
			},
			{
				name: 'authorizationCodeExpiresAt',
				label: '授权code过期时间',
			},
			{
				name: 'authorizationCodeMetadata',
				label: '授权code元数据',
			},
			{
				name: 'accessTokenValue',
				label: '授权access token',
				searchable: true,
			},
			{
				name: 'accessTokenIssuedAt',
				label: '授权access token发布时间',
			},
			{
				name: 'accessTokenExpiresAt',
				label: '授权access token过期时间',
			},
			{
				name: 'accessTokenMetadata',
				label: '授权access token元数据',
			},
			{
				name: 'accessTokenType',
				label: 'access token类型',
			},
			{
				name: 'accessTokenScopes',
				label: 'access token授权范围',
			},
			{
				name: 'oidcIdTokenValue',
				label: '授权oidcId token',
			},
			{
				name: 'oidcIdTokenIssuedAt',
				label: '授权oidcId token发布时间',
			},
			{
				name: 'oidcIdTokenExpiresAt',
				label: '授权oidcId token过期时间',
			},
			{
				name: 'oidcIdTokenMetadata',
				label: '授权oidcId token元数据',
			},
			{
				name: 'refreshTokenValue',
				label: '授权refresh token',
			},
			{
				name: 'refreshTokenIssuedAt',
				label: '授权refresh token发布时间',
			},
			{
				name: 'refreshTokenExpiresAt',
				label: '授权refresh token过期时间',
			},
			{
				name: 'refreshTokenMetadata',
				label: '授权refresh token元数据',
			},
			{
				type: 'date',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
			{
				type: 'date',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
		],
	})
	return <RealTimeCodeEditor code={schema} />
}

export default Oauth2AuthorizationList
